package com.sicnu.cheer.leaugebar.activity.profileset;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.sicnu.cheer.generalmodule.config.AppManager;
import com.sicnu.cheer.generalmodule.util.DateUtil;
import com.sicnu.cheer.generalmodule.util.SharedPreferencesUtil;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.activity.MainActivity;
import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.profileset.R;
import com.sicnu.cheer.profileset.config.Constant;
import com.sicnu.cheer.profileset.utils.ImageUtil;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by cheer on 2017/2/10.
 * 个人资料导航页，用户第一次登录时对个人资料的设置之昵称设置
 */
public class NicknameSetActivity extends BaseActivity implements View.OnClickListener {

    private EditText nickNameET;
    private ImageView sexIV;

    @Override
    public int getContentViewId() {
        return R.layout.activity_nickname_set;
    }

    @Override
    protected void initView() {
        super.initView();
        //展示已选择的性别
        sexIV = ((ImageView) findViewById(R.id.sex_iv));
        ImageUtil.setSexIV(mThis,sexIV);

        nickNameET = ((EditText) findViewById(R.id.nickname_et));
        findViewById(R.id.forward_step_tv).setOnClickListener(this);
        findViewById(R.id.next_step_tv).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.forward_step_tv){
            finish();
        }else if (v.getId()==R.id.next_step_tv){
            //判断昵称是否填写且是否符合要求
            checkNickName();
        }
    }

    private void checkNickName() {
        String nickname=nickNameET.getText().toString();
        if (!StringUtils.isEmpty(nickname)&&nickname.length()<=5) {
            SharedPreferencesUtil.saveUserItem(mThis, Constant.NICKNAME,nickname);

            //更新用户信息
            MyUser newUser =new MyUser();
            newUser.setSex(SharedPreferencesUtil.getUserItem(mThis,Constant.SEX));
            newUser.setBirthday(SharedPreferencesUtil.getUserItem(mThis,Constant.BIRTHDAY));
            newUser.setHometown(SharedPreferencesUtil.getUserItem(mThis,Constant.HOMETOWN));
            newUser.setNick(SharedPreferencesUtil.getUserItem(mThis,Constant.NICKNAME));
            newUser.setInit(true);
            newUser.setManager(false);

            BmobUser currentUser = BmobUser.getCurrentUser(mThis);

            final ProgressDialog dialog=new ProgressDialog(mThis);
            dialog.setMessage("加载中...");
            dialog.show();
            newUser.update(mThis, currentUser.getObjectId(), new UpdateListener() {
                @Override
                public void onSuccess() {
                    dialog.dismiss();
                    UIHelper.ToastMessage(mThis,"个人资料设置成功！");
                    jumpToMainPage();
                }

                @Override
                public void onFailure(int i, String s) {
                    dialog.dismiss();
                    UIHelper.ToastMessage(mThis,"个人资料设置失败："+s);
                }
            });


        }else {
            UIHelper.ToastMessage(mThis,"请设置昵称！");
        }
    }

    private void jumpToMainPage() {
        Intent intent=new Intent(mThis,MainActivity.class);
        startActivity(intent);

        //设置成功后将所有的资料设置activity关闭
        finish();
        AppManager.getAppManager().finishActivity(HometownSetActivity.class);
        AppManager.getAppManager().finishActivity(BirthdaySetActivity.class);
     //   AppManager.getAppManager().finishActivity(SexSetActivity.class);
    }
}
